﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Core.DS
{
    public static partial class experiment
    {
        public static dynamic addOrEditExperiment(
            int? eid, string title, string description,
            DateTime? startTime, DateTime endTime)
        {
            var sql = new Sql.SqlDataContext();

            Sql.DSExperiment e;
            if (eid == null) // Add
            {
                e = new Sql.DSExperiment();
            }
            else // Modify
            {
                e = sql.DSExperiment.FirstOrDefault(row => row.eid == eid);
                if (e == null) return new
                {
                    success = false,
                    msg = "实验不存在"
                };
            }
            e.title = title;
            e.description = description;
            e.startTime = startTime;
            e.endTime = endTime;
            int max = (from ex in sql.DSExperiment
                      orderby e._order descending
                      select ex._order).FirstOrDefault();
            if (max != 0) e._order = max + 1;
            else e._order = 0;
            if (eid == null) sql.DSExperiment.InsertOnSubmit(e);
            sql.SubmitChanges();
            return new
            {
                success = true,
                e.eid
            };
        }
    }
}
